package com.punai.dataanalysis.srlib.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.poi.ExcelUtil;
import com.punai.dataanalysis.srlib.service.GovernmentPriceService;
import com.punai.dataanalysis.srlib.vo.GovernmentPriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dataanalysis/srlib/materialPL/government")
public class GovernmentPriceController extends BaseController {
    @Autowired
    private GovernmentPriceService governmentPriceService;

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:getMainTableList')")
    @GetMapping("/getMainTableList")
    public TableDataInfo getMainTableList(GovernmentPriceVo vo) {
        return getDataTable(governmentPriceService.getMainTableList(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:saveRecord')")
    @GetMapping("/saveRecord")
    public AjaxResult saveRecord(GovernmentPriceVo vo) {
        return AjaxResult.success(governmentPriceService.saveRecord(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:getRecordList')")
    @GetMapping("/getRecordList")
    public TableDataInfo getRecordList(GovernmentPriceVo vo) {
        return getDataTable(governmentPriceService.getRecordList(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:deleteRecord')")
    @GetMapping("/deleteRecord")
    public AjaxResult deleteRecord(GovernmentPriceVo vo) {
        return AjaxResult.success(governmentPriceService.deleteRecord(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:getInfoList')")
    @GetMapping("/getInfoList")
    public TableDataInfo getInfoList(GovernmentPriceVo vo) {
        return getDataTable(governmentPriceService.getInfoList(vo));
    }

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:uploadInfo')")
    @PostMapping("/uploadInfo")
    public AjaxResult uploadInfo(String recordId,MultipartFile uploadFile) {
        String s = governmentPriceService.uploadInfo(recordId, uploadFile);
        if (StrUtil.isNotBlank(s)) {
            return AjaxResult.error(s);
        }
        return AjaxResult.success(s);
    }

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:downloadTemplete')")
    @PostMapping(value = "/downloadTemplete")
    public void downloadTemplete(String recordId, HttpServletResponse response) {
        List<GovernmentPriceVo> list = governmentPriceService.downloadTemplete(recordId);
        ExcelUtil<GovernmentPriceVo> util = new ExcelUtil<>(GovernmentPriceVo.class);
        util.exportExcel(response, list, "Sheet");
    }

    @PreAuthorize("@ss.hasPermi('srlib:governmentPL:downloadGovernmentPL')")
    @PostMapping(value = "/downloadGovernmentPL")
    public void downloadGovernmentPL(GovernmentPriceVo vo, HttpServletResponse response) {
        List<GovernmentPriceVo.ExportMianTable> list = governmentPriceService.downloadGovernmentPL(vo);
        ExcelUtil<GovernmentPriceVo.ExportMianTable> util = new ExcelUtil<>(GovernmentPriceVo.ExportMianTable.class);
        util.exportExcel(response, list, "Sheet");
    }

    @PreAuthorize("@ss.hasPermi('dataanalysis:srlib:governmentDetail')")
    @GetMapping("/getTableListByMaterialId")
    public TableDataInfo getTableListByMaterialId(GovernmentPriceVo vo) {
        startPage();
        return getDataTable(governmentPriceService.getTableListByMaterialId(vo));
    }

    @PreAuthorize("@ss.hasPermi('dataanalysis:srlib:governmentDetail')")
    @GetMapping("/getTjtMapByMaterialId")
    public AjaxResult getTjtMapByMaterialId(GovernmentPriceVo vo) {
        return AjaxResult.success(governmentPriceService.getTjtMapByMaterialId(vo));
    }
}